package com.qilin.hscs.service.impl;

import com.qilin.hscs.dto.HostDto;
import com.qilin.hscs.enums.ResultEnum;
import com.qilin.hscs.pojo.User;
import com.qilin.hscs.repository.UserRepository;
import com.qilin.hscs.service.HostService;
import com.qilin.hscs.pojo.Host;
import com.qilin.hscs.pojo.UserHost;
import com.qilin.hscs.exception.SMException;
import com.qilin.hscs.repository.HostRepository;
import com.qilin.hscs.repository.UserHostRepository;
import com.qilin.hscs.util.ResultVOUntil;
import com.qilin.hscs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class HostServiceImpl implements HostService {

    @Autowired
    private HostRepository hostRepository;

    @Autowired
    private UserHostRepository userHostRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public ResultVO findHostByMac(String mac) {
        Host host = findOneByMac(mac);
        return ResultVOUntil.success(host);
    }

    @Override
    public ResultVO modifyHostNameByMac(String mac,String name) {
        Host host = findOneByMac(mac);
        host.setName(name);
        Host re = hostRepository.save(host);
        return ResultVOUntil.success(re);
    }

    @Override
    public ResultVO delHostByMac(String mac) {
        //todo
        return null;
    }

    @Override
    public Host findOneByMac(String mac) {
        Host host = hostRepository.findByMac(mac);
        if (host == null) {
            throw new SMException(ResultEnum.HOST_NOT_FOUND);
        }
        return host;
    }
}
